package com.cltech.frame.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cltech.frame.shiro.Token;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * Created by Administrator on 2018/9/9.
 */
public class JWTUtil {
    // 过期时间10分钟过期
    private static final long ACCESS_TOKEN_EXPIRE_TIME = 60*60*1000;
    private static final String ACCESS_TOKEN_TYPE = "access_token_type";
    //7天过期
    private static final long REFRESH_TOKEN_EXPIRE_TIME = 7*24*60*60*1000;
    private static final String REFRESH_TOKEN_TYPE = "refresh_token_type";
    /**
     * 校验token是否正确
     * @param token 密钥
     * @return 是否正确
     */
    public static boolean verify(String token, String account, String secret, String userId,String type) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim("account", account)
                    .withClaim("userId",userId)
                    .withClaim("type",type)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }
    public static boolean verifyAccessToken(String token, String account, String secret, String userId) {
        return verify(token,account,secret,userId,ACCESS_TOKEN_TYPE);
    }
    public static boolean verifyRefreshToken(String token, String account, String secret, String userId) {
        return verify(token,account,secret,userId,REFRESH_TOKEN_TYPE);
    }
    /**
     * 获得token中的信息无需secret解密也能获得
     * @return token中包含的用户名
     */
    public static String getAccount(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("account").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
    /**
     * 获得token中的信息无需secret解密也能获得
     * @return token中包含的用户id
     */
    public static String getUserId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userId").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 生成签名
     * @param account 用户名
     * @return 加密的token
     */
    public static Token sign(String account, String secret, String userId, String type,
                             Long expireTime) {
        try {
            Date date = new Date(System.currentTimeMillis()+expireTime);
            Algorithm algorithm = Algorithm.HMAC256(secret);
            // 附带account信息
            return new Token(JWT.create()
                    .withClaim("account", account)
                    .withClaim("userId",userId)
                    .withClaim("type",type)
                    .withExpiresAt(date)
                    .sign(algorithm),date.getTime());
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    /**
     * 生成签名
     * @param account 用户名
     * @return 加密的token
     */
    public static Token accessToken(String account, String secret, String userId) {
        return sign(account,secret,userId,ACCESS_TOKEN_TYPE,ACCESS_TOKEN_EXPIRE_TIME);
    }
    /**
     * 生成签名
     * @param account 用户名
     * @return 加密的token
     */
    public static Token refreshToken(String account, String secret, String userId) {
        return sign(account,secret,userId,REFRESH_TOKEN_TYPE,REFRESH_TOKEN_EXPIRE_TIME);
    }
}
